<?php
require  APPPATH."/libraries/Uploads.php";

$type=$_REQUEST['type'];
if(empty($type)){
    $type="test";
}
$upload_type=$_FILES['file']['type'];
$checked=$this->check_type($type,$upload_type);
if($checked){
    //不能上传空文件
    if(empty($_FILES["file"]) || !file_exists($_FILES["file"]["tmp_name"])){
        response(-1,"文件不能为空");
    }
    try{
        //解决跨域问题
        $this->config->load("upload");
        $uploadConfigs = config_item("upload");
        //创建上传目录，配置上传
        $date1=date("Y",time());
        $date2=date("m",time());
        $date3=date("d",time());
        $dir="/".$date1."/".$date2."/".$date3;
        $uploadpath1=$uploadConfigs[$type]['uploadpath'];
        $uploadpath=$uploadpath1.$dir;
        $config['filename']=$uploadConfigs[$type]['filename'];
        $config['uploadpath']=$uploadpath;
        $config['allowtype']=$uploadConfigs[$type]['allowtype'];
        $config['types']=$type;
        $config['savehttp']=$uploadConfigs[$type]['savehttp'].$dir;
//                var_dump($config);exit;
        //开启上传
        $singleUpload=new Uploads($config);
        $da=$singleUpload->doupload();
        $fileInfo['file_name'] = $singleUpload->getFinalFileName();
        $fileInfo['file_url'] = $singleUpload->savehttp();
        //上传成功
        $this->output(0,"ok",$fileInfo);
    }catch (Exception $e){
        $this->output(-1,"上传失败msg==>".$e->getMessage());
    }

}else{
    $this->output(-1,"暂时不支持该类型上传");
}


/**
 * 检验上传的类型
 */
 function check_type($type,$upload_type){
    //允许上传类型
    $video=["video/mp4","video/mov","video/avi"];
    $image=["image/png","image/jpeg","image/jpg","image/gif"];
    $audio=["audio/mp3","audio/aac"];
    //检测上传类型
    if($type=="image"){
        if(in_array($upload_type,$image)){
            return true;
        }else{
            return false;
        }
    }else if($type=="audio"){
        if(in_array($upload_type,$audio)){
            return true;
        }else{
            return false;
        }
    }else if($type=="video"){
        if(in_array($upload_type,$video)){
            return true;
        }else{
            return false;
        }
    }else if($type=="test"){

        return true;

    } else{
        return false;
    }
}